package com.kakarote.oa.controller;


import cn.hutool.core.util.StrUtil;
import com.kakarote.common.field.utils.FieldUtil;
import com.kakarote.core.common.Result;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.examine.service.ExamineService;
import com.kakarote.oa.entity.BO.OaFieldVerifyBO;
import com.kakarote.oa.entity.PO.OaExamine;
import com.kakarote.oa.entity.PO.OaExamineData;
import com.kakarote.oa.entity.PO.OaExamineField;
import com.kakarote.oa.service.IOaExamineDataService;
import com.kakarote.oa.service.IOaExamineFieldService;
import com.kakarote.oa.service.IOaExamineService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * oa审批自定义字段存值表 前端控制器
 * </p>
 *
 * @author wyq
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/oaExamineData")
public class OaExamineDataController {
    @Autowired
    private IOaExamineDataService examineDataService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private IOaExamineFieldService examineFieldService;

    @Autowired
    private IOaExamineService oaExamineService;

    @PostMapping("/verify")
    @ApiOperation("验证字段是否存在")
    public Result verify(@RequestBody OaFieldVerifyBO examineDataBO) {
        List<Long> data = examineService.queryHistoryExamineId(examineDataBO.getDataId()).getData();
        if (data.isEmpty()) {
            throw new CrmException();
        }
        List<OaExamine> oaExamineList = oaExamineService.lambdaQuery().in(OaExamine::getCategoryId, data).ne(OaExamine::getExamineStatus, 8).list();
        List<String> batchIds = oaExamineList.stream().map(OaExamine::getBatchId).collect(Collectors.toList());

        if (examineDataBO.getType() == 1) {
            switch (examineDataBO.getFieldName()) {
                case "content":
                    oaExamineList.stream().map(OaExamine::getContent).forEach(content -> {
                        if (StrUtil.isNotBlank(content) && content.equals(examineDataBO.getValue())) {
                            examineDataBO.setStatus(1);
                        }
                    });
                    break;
                case "remark":
                    oaExamineList.stream().map(OaExamine::getRemark).forEach(remark -> {
                        if (StrUtil.isNotBlank(remark) && remark.equals(examineDataBO.getValue())) {
                            examineDataBO.setStatus(1);
                        }
                    });
                    break;
            }
        } else {
            List<OaExamineField> list = examineFieldService.lambdaQuery()
                    .select(OaExamineField::getFieldId, OaExamineField::getType)
                    .in(OaExamineField::getExamineCategoryId, data)
                    .in(OaExamineField::getFieldName, StrUtil.toUnderlineCase(examineDataBO.getFieldName()),examineDataBO.getFieldName())
                    .list();

            OaExamineField oaExamineField = list.stream().findFirst().orElseThrow(CrmException::new);
            List<Long> fieldIds = list.stream().map(OaExamineField::getFieldId).collect(Collectors.toList());
            String value = FieldUtil.format2DbString(examineDataBO.getValue(), oaExamineField.getType(), examineDataBO.getValue());
            if (StrUtil.isNotBlank(value)) {
                Long count = examineDataService.lambdaQuery()
                        .in(OaExamineData::getFieldId, fieldIds).eq(OaExamineData::getValue, value)
                        .in(OaExamineData::getBatchId, batchIds)
                        .ne(StrUtil.isNotEmpty(examineDataBO.getBatchId()), OaExamineData::getBatchId, examineDataBO.getBatchId())
                        .count();
                if (count > 0) {
                    examineDataBO.setStatus(1);
                }
            }
        }
        return Result.ok(examineDataBO);
    }

}

